package com.duobaoyu.dby.supply.biz.client.product.impl;

import com.duobaoyu.dby.channel.adapter.enums.biz.product.PublishStatusEnum;
import com.duobaoyu.dby.common.core.constant.SecurityConstants;
import com.duobaoyu.dby.common.core.constant.ServiceNameConstants;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.saas.product.api.feign.RemoteSaasProductSkuStockApiService;
import com.duobaoyu.dby.saas.product.api.feign.RemoteSaasSupplyProductApiService;
import com.duobaoyu.dby.saas.product.common.remote.dto.PlatformPriceCheckDTO;
import com.duobaoyu.dby.saas.product.common.remote.dto.SkuStockBatchStatusDTO;
import com.duobaoyu.dby.saas.product.common.remote.dto.SkuStockOrderNumDTO;
import com.duobaoyu.dby.saas.product.common.remote.dto.SkuStockQueryDTO;
import com.duobaoyu.dby.saas.product.common.remote.to.BatchUpdateTO;
import com.duobaoyu.dby.saas.product.common.remote.to.PlatformPriceCheckTO;
import com.duobaoyu.dby.saas.product.common.remote.to.SkuStockInfoTO;
import com.duobaoyu.dby.supply.biz.client.product.ProductCenterApiClient;
import com.duobaoyu.dby.supply.biz.client.product.bean.bo.PlatformPriceCheckBO;
import com.duobaoyu.dby.supply.biz.client.product.bean.bo.SkuInfoCheckBO;
import com.duobaoyu.dby.supply.biz.client.product.converter.ProductConverter;
import com.duobaoyu.dby.supply.biz.util.RemoteResultCheckUtil;
import com.duobaoyu.dby.supply.common.biz.remote.dto.order.SubmitOrderDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品中心调用
 *
 * @author fengshaoyu
 * @version 1.0.0
 * @date 2023/10/7 13:50
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ProductCenterApiClientImpl implements ProductCenterApiClient {
	private final ProductConverter productConverter;
	private final RemoteSaasSupplyProductApiService remoteSaasSupplyProductApiService;
	private final RemoteSaasProductSkuStockApiService remoteSaasProductSkuStockApiService;

	@Override
	public SkuInfoCheckBO skuInfoCheck(List<SubmitOrderDTO.SkuInfo> skuInfos, String serverName, Boolean checkPriceFlag) {
		List<SkuStockQueryDTO.SkuInfo> skuInfoCheck = productConverter.toSkuStockQueryDTO(skuInfos);
		SkuStockQueryDTO skuStockQuery = new SkuStockQueryDTO();
		skuStockQuery.setServerName(serverName);
		skuStockQuery.setCheckPrice(checkPriceFlag);
		skuStockQuery.setSkuInfoList(skuInfoCheck);
		R<SkuStockInfoTO> result = remoteSaasProductSkuStockApiService.checkSkuStock(skuStockQuery, SecurityConstants.FROM_IN);
		RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.SAAS_PRODUCT_API_SERVICE, skuStockQuery);
		List<SkuInfoCheckBO.ErrorSkuInfo> errorSkuInfoList = productConverter.toSkuInfoCheckErrorSkuList(result.getData().getErrorSkuInfoList());
		List<SkuInfoCheckBO.SkuInfo> skuInfoList = productConverter.toSkuInfoCheckSkuList(result.getData().getSkuInfoList());
		return SkuInfoCheckBO.builder().errorSkuInfoList(errorSkuInfoList).skuInfoList(skuInfoList).build();
	}

	@Override
	public void offShelfProducts(List<String> skuCodes) {
		try {
			R<BatchUpdateTO> result = remoteSaasSupplyProductApiService.batchStatus(SkuStockBatchStatusDTO.builder().skuCodes(skuCodes)
					.publishStatus(PublishStatusEnum.TAKE_DOWN_BY_THIRD.getValue()).build(), SecurityConstants.FROM_IN);
			RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.SAAS_PRODUCT_API_SERVICE, skuCodes);
		} catch (Exception e) {
			log.warn("message:{},异常:{}", "下架商品失败", ExceptionUtils.getStackTrace(e));
		}
	}

	@Override
	public PlatformPriceCheckBO checkPlatformPriceAndState(List<PlatformPriceCheckDTO> platformPriceCheckList) {
		R<PlatformPriceCheckTO> result = remoteSaasProductSkuStockApiService.checkPlatformPriceAndState(platformPriceCheckList, SecurityConstants.FROM_IN);
		RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.SAAS_PRODUCT_API_SERVICE, platformPriceCheckList);
		return productConverter.toPlatformPriceCheck(result.getData());
	}

	@Override
	public void batchOrderNumUpdate(List<SkuStockOrderNumDTO> skuStockOrderNumList) {
		try {
			R<Boolean> result = remoteSaasProductSkuStockApiService.updateSkuStockOrderNum(skuStockOrderNumList, SecurityConstants.FROM_IN);
			RemoteResultCheckUtil.checkRemoteResult(result, ServiceNameConstants.SAAS_PRODUCT_API_SERVICE, skuStockOrderNumList);
		} catch (Exception e) {
			log.warn("message:{},异常:{}", "加减销量调用失败", ExceptionUtils.getStackTrace(e));
		}
	}
}
